//
//  YWNoticeAndTempMissionCell.m
//  maintain
//
//  Created by 郑想 on 16/7/22.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNoticeAndTempMissionCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "GropPictureView.h"
#import "YWGropButtonView.h"
#import "YWNotice.h"
#import "YWCommonTool.h"
#import "YWHandler.h"
#import "UIImageView+WebCache.h"
#import "YWKeepWatchMissionManager.h"
#import "YWAttachmentManager.h"
#import "YWReplyManager.h"
#import "YWTempMission.h"

@interface YWNoticeAndTempMissionCell()<YWGropButtonViewDelegate>
/** 头像 */
@property (nonatomic, strong) UIImageView *headPortraitView;
/** 名字 */
@property (nonatomic, strong) UILabel *nameLabel;
/** 时间 */
@property (nonatomic, strong) UILabel *timeLabel;
/** 接收部门，机构 */
@property (nonatomic, strong) UILabel *receiverLabel;
/** 标题 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 编号 */
@property (nonatomic, strong) UILabel *numberLabel;
/** 内容 */
@property (nonatomic, strong) UILabel *contentLabel;
/** 全文按钮 */
@property (nonatomic, strong) UIButton *contentBut;
/** 图片容器 */
@property (nonatomic, strong) GropPictureView *pictureGroupContentView;
/** 文件容器 */
@property (nonatomic, strong)  YWGropButtonView *fileContentView;
/** 右下角回复按钮 */
@property (nonatomic, strong) UIButton *replyBnt;
/** 请于?时完成 */
@property (nonatomic, strong) UILabel *finishedTimeLabel;

@end

@implementation YWNoticeAndTempMissionCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    YWNoticeAndTempMissionCell *cell = [tableView dequeueReusableCellWithIdentifier:YWNoticeAndTempMissionCellIdentifier];
    if (cell == nil) {
        cell = [[YWNoticeAndTempMissionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:YWNoticeAndTempMissionCellIdentifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
//        self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
    }
    return self;
}


- (void)setNotice:(YWNotice *)notice{
    _notice = notice;
    
        self.fileContentView.delegate = self;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:notice.fromUser.headIcon];
    //    头像
    [self.headPortraitView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    //   时间
    NSString *timeStr = [YWCommonTool showTimeWithNormalFormatter:notice.createTime/1000];
    self.timeLabel.text = timeStr;
    //    名字
    self.nameLabel.text = notice.fromUser.userName ?:@" ";
    //机构部门
    self.receiverLabel.text = notice.receivers?:@" ";
    
    //    标题
    self.titleLabel.text = notice.title;
    //    图片集
    NSArray *pictures = [[YWKeepWatchMissionManager shareManager]recordPictureWithRecordId:[NSString stringWithFormat:@"%ld",(long)self.notice.noticeId] recordType:YWPictureRecordTypeNotice];
    if (pictures.count == 0) {
        [_pictureGroupContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0).priorityHigh();
        }];
    } else if(pictures.count <= 3){
        [_pictureGroupContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH - 70)/3 + 5)).priorityHigh();
        }];
        [self.pictureGroupContentView initWithGroupItems:pictures withConstraint:nil];
    } else {
        [_pictureGroupContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH - 70)/3 * 2 + 10)).priorityHigh();
        }];
        [self.pictureGroupContentView initWithGroupItems:pictures withConstraint:nil];
    }
    //    文件集
    NSArray *attachments = [[YWAttachmentManager shareManager]attachmentWithRecordId:self.notice.noticeId andType:YWattachmentTypeOfNotice];
    if (attachments.count == 0) {
        [_fileContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0).priorityHigh();
        }];
    } else if (attachments.count <= 3){
        [_fileContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH - 70)/3 + 5)).priorityHigh();
        }];
        [self.fileContentView initWithGroupItems:attachments withshotContrain:nil];
    } else {
        [_fileContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH - 70)/3 * 2 + 10)).priorityHigh();
        }];
        [self.fileContentView initWithGroupItems:attachments withshotContrain:nil];
    }
    //    评论 个数
    NSInteger replyCount = [[YWReplyManager shareManager] replysCountWithtRelationId:self.notice.noticeId relationType:YWReplyRelationTypeNotice];
    [self.replyBnt setTitle:@(replyCount).stringValue forState:UIControlStateNormal];
    //编号
    self.numberLabel.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"code", nil),notice.workSequence ?: @""];
    //    内容
    if (notice.content.length > 100 && _showExpendBtn) {
        [_contentBut mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@15);
            make.top.equalTo(_contentLabel.mas_bottom).offset(6);
        }];
        _contentBut.hidden = NO;
        self.contentLabel.text = [NSString stringWithFormat:@"%@...",[notice.content substringToIndex:100]];
    }else{
        [_contentBut mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@0);
            make.top.equalTo(_contentLabel.mas_bottom).offset(0);
        }];
        _contentBut.hidden = YES;
        self.contentLabel.text = self.notice.content?:@"";
    }
    if (_showExpendBtn) {
        self.pictureGroupContentView.userInteractionEnabled = NO;
        self.fileContentView.userInteractionEnabled = NO;
    }
    
    self.receiverLabel.preferredMaxLayoutWidth = self.receiverLabel.frame.size.width;
    self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.frame.size.width;
    self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.frame.size.width;
}

#pragma mark - TempMission
- (void)setTempMission:(YWTempMission *)tempMission{
    _tempMission = tempMission;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:_tempMission.fromUser.headIcon];
    self.fileContentView.delegate = self;
    //    头像
    [_headPortraitView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    NSString *fromMan = tempMission.fromUser.userName;
    //    名字
    _nameLabel.text = fromMan;
    
    NSString *timeStr = [YWCommonTool showTimeWithNormalFormatter:tempMission.createTime/1000];
    _timeLabel.text = timeStr;
    //机构部门
    NSString *executors = [YWCommonTool handlerNamesWithHandlers:tempMission.executors];
    _receiverLabel.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"executor", nil),executors];
    
    _titleLabel.text = tempMission.title;
    //编号
    _numberLabel.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"code", nil),tempMission.workSequence ?: @""];
    //    内容
    if (tempMission.content.length > 100 && _showExpendBtn) {
        [_contentBut mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@15);
            make.top.equalTo(_contentLabel.mas_bottom).offset(6);
        }];
        _contentBut.hidden = NO;
        self.contentLabel.text = [NSString stringWithFormat:@"%@...",[tempMission.content substringToIndex:100]];
    }else{
        [_contentBut mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@0);
            make.top.equalTo(_contentLabel.mas_bottom).offset(0);
        }];
        _contentBut.hidden = YES;
        self.contentLabel.text = tempMission.content?:@"";
    }
    NSString *completeTime = [YWCommonTool showTime:tempMission.completionTime/1000 andFormatter:KTimeFormatMonthDayAndTime];
    _finishedTimeLabel.text = [NSString stringWithFormat:@"%@%@前完成",NSLocalizedString(@"pleaseAt", nil),completeTime];
    //    图片集
    NSArray *pictures = [[YWKeepWatchMissionManager shareManager]recordPictureWithRecordId:[NSString stringWithFormat:@"%ld",(long)tempMission.tempMissionId] recordType:YWPictureRecordTypeTempMission];
    if (pictures.count == 0) {
        [_pictureGroupContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0).priorityHigh();
        }];
    } else if(pictures.count <= 3){
        [_pictureGroupContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH - 70)/3 + 5)).priorityHigh();
        }];
        [self.pictureGroupContentView initWithGroupItems:pictures withConstraint:nil];
    } else {
        [_pictureGroupContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH - 70)/3 * 2 + 10)).priorityHigh();
        }];
        [self.pictureGroupContentView initWithGroupItems:pictures withConstraint:nil];
    }    //  文件集
    NSArray *attachments = [[YWAttachmentManager shareManager]attachmentWithRecordId:tempMission.tempMissionId andType:YWattachmentTypeOfTempMission];
    if (attachments.count == 0) {
        [_fileContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0);
        }];
    } else if (attachments.count <= 3){
        [_fileContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH - 70)/3 + 5));
        }];
        [self.fileContentView initWithGroupItems:attachments withshotContrain:nil];
    } else {
        [_fileContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH - 70)/3 * 2 + 10));
        }];
        [self.fileContentView initWithGroupItems:attachments withshotContrain:nil];
    }
    //    评论
    NSInteger replyCount = [[YWReplyManager shareManager] replysCountWithtRelationId:_tempMission.tempMissionId relationType:YWReplyRelationTypeTempMission];
    [_replyBnt setTitle:@(replyCount).stringValue forState:UIControlStateNormal];
    if (_showExpendBtn) {
        _pictureGroupContentView.userInteractionEnabled = NO;
        _fileContentView.userInteractionEnabled = NO;
    }
    
    self.receiverLabel.preferredMaxLayoutWidth = self.receiverLabel.frame.size.width;
    self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.frame.size.width;
    self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.frame.size.width;
}


- (void)setUpSubviews{
    [self.headPortraitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(self.contentView).offset(8);
        make.width.and.height.mas_equalTo(@46);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_headPortraitView.mas_centerY).offset(-2);
        make.left.mas_equalTo(_headPortraitView.mas_right).offset(6);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLabel.mas_left);
        make.height.mas_equalTo(18);
        make.top.mas_equalTo(_nameLabel.mas_bottom).offset(4);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.receiverLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_timeLabel.mas_bottom).offset(4);
        make.left.mas_equalTo(_timeLabel.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_headPortraitView.mas_centerX);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.top.mas_equalTo(_receiverLabel.mas_bottom).offset(10);
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(_titleLabel);
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(6);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(_titleLabel);
        make.top.mas_equalTo(_numberLabel.mas_bottom).offset(6);
    }];
    
    [self.contentBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel);
        make.top.mas_equalTo(_contentLabel.mas_bottom).offset(6);
    }];
    
    [self.pictureGroupContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(_titleLabel).priorityHigh();
        make.top.mas_equalTo(_contentBut.mas_bottom).offset(6).priorityHigh();
        make.height.mas_equalTo(@90).priorityHigh();
    }];
    
    [self.fileContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(_titleLabel).priorityHigh();
        make.top.mas_equalTo(_pictureGroupContentView.mas_bottom).priorityHigh();
        make.height.mas_equalTo(@90).priorityHigh();
    }];
    
    [self.replyBnt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.and.bottom.mas_equalTo(self.contentView);
        make.top.mas_equalTo(_fileContentView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    
    [self.finishedTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_replyBnt.mas_centerY);
        make.right.mas_equalTo(_replyBnt.mas_left);
    }];
    
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}

- (UIImageView *)headPortraitView{
    if (!_headPortraitView) {
        UIImageView *imageView = [UIImageView new];
        imageView.backgroundColor = [UIColor customTextGrayColor];
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 46/2;
        _headPortraitView = imageView;
        [self.contentView addSubview:_headPortraitView];
    }
    return _headPortraitView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 1;
        label.textColor = [UIColor blackColor];
        _nameLabel = label;
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _timeLabel = label;
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (UILabel *)receiverLabel{
    if (!_receiverLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayColor];
        _receiverLabel = label;
        [self.contentView addSubview:_receiverLabel];
    }
    return _receiverLabel;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        _titleLabel = label;
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)numberLabel{
    if (!_numberLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 1;
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _numberLabel = label;
        [self.contentView addSubview:_numberLabel];
    }
    return _numberLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _contentLabel = label;
        [self.contentView addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (UIButton *)contentBut{
    if (!_contentBut) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTintColor:[UIColor customBlueColor]];
        [button setTitle:NSLocalizedString(@"allText", nil) forState:UIControlStateNormal];
        button.userInteractionEnabled = NO;
        button.titleLabel.font = kContentFont;
        _contentBut = button;
        [self.contentView addSubview:_contentBut];
    }
    return _contentBut;
}

-(GropPictureView *)pictureGroupContentView{
    if (!_pictureGroupContentView) {
        GropPictureView *gropPictureView = [GropPictureView new];
        _pictureGroupContentView = gropPictureView;
        [self.contentView addSubview:_pictureGroupContentView];
    }
    return _pictureGroupContentView;
}

-(YWGropButtonView *)fileContentView{
    if (!_fileContentView) {
        YWGropButtonView *gropButtonView = [YWGropButtonView new];
        _fileContentView = gropButtonView;
        [self.contentView addSubview:_fileContentView];
    }
    return _fileContentView;
}

-(UIButton *)replyBnt{
    if (!_replyBnt) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = kContentFont;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"remind_replyIcon"] forState:UIControlStateNormal];
        button.layer.masksToBounds = YES;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        _replyBnt = button;
        [self.contentView addSubview:_replyBnt];
    }
    return _replyBnt;
}

- (UILabel *)finishedTimeLabel{
    if (!_finishedTimeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 1;
        label.font = kContentFont;
        label.textColor = [UIColor customRedColor];
        _finishedTimeLabel = label;
        [self.contentView addSubview:_finishedTimeLabel];
    }
    return _finishedTimeLabel;
}

#pragma mark - YWGropButtonViewDelegate
-(void)handleAttachment:(YWattachment *)attachment{
    if ([self.delegate respondsToSelector:@selector(showAttachment:)]) {
        [self.delegate showAttachment:attachment];
    }
}

@end
